var User = require('../model/user')
var Shop = require('../model/shop')
var Goods = require('../model/goods')
var Like = require('../model/like')
var SuccessCode = require('./successCode')
var ErrorCode = require('./errorCode')

var goodsApi = {
        createGoods:function(req,res){
            var icon = req.files.icon.path
            var shopid = req.body.shopid
            Shop.findOne({_id:shopid},function(err,shop){
                if (!shop){
                    res.json(400,ErrorCode.CommonError)
                    return
                }else{
                    var goods = {
                        name:req.body.name,
                        icon:icon,
                        count:req.body.count,
                        info:req.body.info,
                        shopid:shop._id,
                        price:req.body.price,
                        likeCount:0,
                        type:req.body.type,
                        off:false
                    }
                    Goods.create(goods,function(err,g){
                        if(err){
                            res.json(400,ErrorCode.CommonError)
                            return
                        }else{
                            res.json(g)
                            return
                        }                
                    })
                }
            })
        },
        listGoodsByType:function(req,res){
            var type = req.query.type
            var shopid = req.query.shopid
            Goods.find({shopid:shopid,type:type},function(err,goods){
                res.json(goods)
                return
            })
        },
        listGoodsById:function(req,res){
            var goodsid = req.query.goodsId
            Goods.findOne({_id:goodsid},function(err,goods){
                res.json(goods)
                return
            })
        }
        ,
        listGoods:function(req,res){
            var shopid = req.query.shopid
            Goods.find({shopid:shopid},function(err,goods){
                res.json(goods)
                return
            })
            
        }
        ,
        offGoods:function(req,res){
            var goodsid = req.query.id
            Goods.findOne({_id:goodsid},function(err,goods){
                goods.off = true
                goods.save(function(err){
                    res.json(goods)
                    return
                })
            })
        },
        onGoods:function(req,res){
            var goodsid = req.query.id
            Goods.findOne({_id:goodsid},function(err,goods){
                goods.off = false
                goods.save(function(err){
                    res.json(goods)
                    return
                })
            })
        },
        updateGoods:function(req,res){
            var iconB = req.files.icon
            var icon
            if(iconB){
                icon = iconB.path
            }

            var goodsid = req.body.id
            var name = req.body.name
            
            var count = req.body.count
            var info = req.body.info
            var price = req.body.price
            var type = req.body.type

            Goods.findOne({_id:goodsid},function(err,goods){
                if(err){
                    res.json(400,err)
                    return
                }
                if (name){
                    goods.name = name
                }
                if(icon){
                    goods.icon = icon
                }
                if(typeof(count) != "undefined"){
                    goods.count = count
                }
                if(info){
                    goods.info = info
                }
                if(price){
                    goods.price = price
                }
                if(type){
                    goods.type = type
                }
                
                
                goods.save(function(err){
                    if(err){
                        res.json(400,err)
                        return
                    }
                    res.json(goods)
                    return
                })
            }) 
        },
        like:function(req,res){
            var id = req.body.id
            var userid = req.userid
            Goods.findOne({_id:id},function(err,goods){
                Like.findOne({userId:userid,goodsId:id},function(err,like){
                    if(like){
                        res.json(err)
                    }else{
                        Like.create({userId:userid,goodsId:id},function(err,like){
                            if(!err){
                                goods.count += 1
                                goods.save()
                            }
                            res.json(err)
                        })
                    }
                })
            })
        },
        unlike:function(req,res){
            var id = req.body.id
            var userid = req.userid
            Goods.findOne({_id:id},function(err,goods){
                
                Like.remove({userId:userid,goodsId:id},function(err){
                    if(!err){
                        goods.count -= 1
                        goods.save()
                    }
                    res.json(err)
                })
            })
        }
        


}
module.exports = goodsApi